import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 蒋健
 * Date: 2022-01-23
 * Time: 10:02
 */
public class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if (!minStack.empty()) {
            int top = minStack.peek();
            //比较  等于也要放进去
            if (val <= top) {
                minStack.push(val);
            }
        } else {
            minStack.push(val);
        }
    }

    public void pop(){
        int popVal =stack.pop();
        if(!minStack.empty()){
            int top=minStack.peek();
            if(top==popVal){
                minStack.pop();
            }
        }
    }

    public int top(){
        return stack.peek();
    }

    public int getMin(){
        return minStack.peek();
    }

}
